/**
 * 
 */
package gui;

import java.util.Vector;

import netrs.Config;
import netrs.Receptor;
import process.Pool;
import process.SystemCall;

/**
 * @author eduardo
 *
 */
public class HtmlSpools {

	public static final int COL_NAME=0;
	public static final int COL_WGET_T00=1;
	public static final int COL_T00_DAT=2;
	public static final int COL_DAT_RINEX=3;
	public static final int COL_DELETE_DAT=4;
	public static final int COL_RINEX_QUALITY=5;
	public static final int COL_GZIP_RINEX=6;
	public static final int COL_TOTAL=7;


	private static final String[] columnNames = {
		"Station",
		"WGET_T00",
		"T00_DAT",
		"DAT_RINEX",
		"DELETE_DAT",
		"RINEX_QUALITY",
		"RINEX_COMPRESS",
	"Total"};

	private static final String[] columnToolTips = {
		"'System Name'.",
		"Wget Processes.",
		"Runpkr Processes.",
		"Teqc Processes.",
		"Delete X.dat Processes.",
		"Rinex Quality.",
		"Rinex Gzip.",
		"Totals."
	};

	public String generaTablaWget()
	{
		StringBuilder tabla;
		tabla = new StringBuilder ();
		tabla.append("<table border='1' cellpadding='10'><th>WGET</th>");
		Pool wgetPool;
		Vector<SystemCall> llamadasWget;
		SystemCall scLocal;
		wgetPool = Config.getLocker().get("WGET_T00");
		llamadasWget = wgetPool.getAgenda();
		int largo, i;
		largo = llamadasWget.size();
		for (i=0;i<largo;i++)
		{

			scLocal = llamadasWget.elementAt(i);


			tabla.append("<tr><td>");
			tabla.append( scLocal.getFeedBack() );
			tabla.append("</td></tr>");
		}
		tabla.append("</table>");
		return tabla.toString();
	}

	public String generaTabla()
	{
		int cantFilas, cantColumnas, row;
		short column;
		cantColumnas = columnNames.length;
		StringBuilder tabla;
		tabla = new StringBuilder ();
		tabla.append("<table  border='1' cellpadding='10'");
		tabla.append(">\n");
		/// A continuacion mostramos los encabezados de las columnas
		tabla.append("<tr>\n");
		Pool pool;
		for (column=0;column<cantColumnas;column++)
		{
			pool = Config.getLocker().get(columnNames[column]);
			if  ((column==COL_NAME) || (pool!=null)) 
			{
				tabla.append("<th title=\"");
				tabla.append(columnToolTips[column]);
				tabla.append("\">\n");
				tabla.append(columnNames[column]);
				tabla.append("</th>\n");
			}
		}
		cantFilas = Config.getVReceptor().size();
		Receptor receptor;
		// Total de procesos actuales, sumando los valores de la fila
		int totalCurrentFila;
		int currentCelda;
		int maxCelda;
		String serialNumber, nombreReceptor;
		for (row= 0;row<cantFilas;row++)
		{
			totalCurrentFila = 0;
			tabla.append("<tr align='center'>\n");
			for (column=0;column<cantColumnas;column++)
			{
				pool = Config.getLocker().get(columnNames[column]);
				if  ((column==COL_NAME) || (pool!=null)) 
				{
					tabla.append("<td>\n");
					receptor = Config.getVReceptor().elementAt(row);
					nombreReceptor = receptor.getSystemName();
					switch (column) {
					case COL_NAME:
						tabla.append(nombreReceptor);
						tabla.append("<br>Sesiones:<br>");
						tabla.append(receptor.getStrSortFilter());
						break;
					case COL_WGET_T00:
					case COL_T00_DAT:
					case COL_DAT_RINEX:
					case COL_DELETE_DAT:
					case COL_RINEX_QUALITY:
					case COL_GZIP_RINEX:
						serialNumber = receptor.getSerialNumber();

						for (SystemCall syscall : pool.getCurrentClientProcess().get(serialNumber)) {
							tabla.append(syscall.getFeedBack());
							tabla.append("<br>");
						}

						currentCelda =pool.getCurrentClientProcess().get(serialNumber).size();
						maxCelda = pool.getMaxClientProcess().get(serialNumber);
						totalCurrentFila += currentCelda;
						tabla.append(currentCelda);
						tabla.append("<br>max[");
						tabla.append(nombreReceptor);
						tabla.append("]=");
						tabla.append(maxCelda);
						tabla.append("<br>last finished=<br>");
						tabla.append(pool.getLastFinished().get(serialNumber));
						break;
					case COL_TOTAL:
						tabla.append(totalCurrentFila);
						break;

					default:
						break;
					}

					tabla.append("</td>\n");
				}
			}
			tabla.append("</tr>\n");
		}
		tabla.append("<tr>\n");

		for (column=0;column<cantColumnas;column++)
		{
			pool = Config.getLocker().get(columnNames[column]);
			if  ((column==COL_NAME) || (pool!=null)) 
			{
				tabla.append("<td>\n");
				switch (column) {
				case COL_NAME:
					tabla.append("TOTAL");
					break;
				case COL_WGET_T00:
				case COL_T00_DAT:
				case COL_DAT_RINEX:
				case COL_DELETE_DAT:				
				case COL_RINEX_QUALITY:
				case COL_GZIP_RINEX:

					tabla.append(pool.getCountRunningProcesos());
					tabla.append("<br/>max[");
					tabla.append(columnNames[column]);
					tabla.append("]=");
					tabla.append(pool.getMaxRunningProcesos());

					break;

				case COL_TOTAL:
					tabla.append(".");
					break;
				}
				tabla.append("</td>\n");			
			}
		}
		tabla.append("</tr>\n");
		tabla.append("</table>");
		return tabla.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
